#include "ReceptorMovs.h"
#include "user.h"

ReceptorMovs::ReceptorMovs(Socket* sock, bool& boolWin, Mutex& m, ConditionVariable& cv):
stopWindow(boolWin), mut(m), condVar(cv){
    this->socket = sock;
	this->running = false;
}
void ReceptorMovs::startMethod(){
	//std::cout<<"Lanzo thread"<<std::endl;
	this->running = true;
	User* user = User::getInstance();
	while(this->running){
		//std::cout<<"Por recibir"<<std::endl;
		Json::Value json;
		if (this->socket->recibir(json,user->getPass()) == 0){
			this->running = false;
			break;
		}
		this->mut.lock();
		this->recibido.push(json);
		this->condVar.broadcast();
		this->mut.unlock();
		// if (json["Accion"].asString() != "Mostrar tablero"){
		// 	std::cout<<"Recibio info: "<<json<<std::endl;
		// }
		this->stopWindow = true;
	}
}

void ReceptorMovs::finish(){
	this->running = false;
	this->socket->shutDown(2);
}

bool ReceptorMovs::isRunning(){
	return this->running;
}

bool ReceptorMovs::hayDatos(){
	return !this->recibido.empty();
}

void ReceptorMovs::getDato(Json::Value& json){
	if (this->recibido.empty()){
		//std::cout<<"Vacio"<<std::endl;
		return;
	}
	json =  this->recibido.front();
	//std::cout<<"json: "<<json<<std::endl;
	this->recibido.pop();
}

ReceptorMovs::~ReceptorMovs(){
	this->stopWindow = false;
}
